package com.red.servlet.user;

import com.red.entity.User;
import com.red.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserUpdateServlet", value = "/updateUser")

public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        int id = Integer.parseInt(req.getParameter("uID"));
        String name = req.getParameter("uName");
        String account = req.getParameter("uAccount");
        String password = req.getParameter("uPassword");
        int level = Integer.parseInt(req.getParameter("uLevel"));
        String email = req.getParameter("uEmail");

        User user = new User(name, account, password, email, level);
        user.setuID(id);

        String info;

        if (new UserServiceImpl().update(user) > 0) {
            info = String.format("已成功修改了 %s 的信息", name);
        } else {
            info = "修改失败";
        }
        req.setAttribute("updateResult", info);
        req.getRequestDispatcher("user_manage").forward(req, resp);
    }
}
